package com.vincent.kdp.http;

import android.provider.Settings;
import android.util.Log;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.SyncHttpClient;
import com.vincent.baseapp.util.view.LoadingView;
import com.vincent.kdp.ui.bean.response.BaseOutput;
import com.vincent.kdp.util.BaseParam;

import org.apache.http.Header;


/**
 * Created by Administrator on 2015/5/23.
 */
public class HttpUtil {
    private static final String TAG = "HttpUtil";
    private static LoadingView loadingView;
    private static HttpUtil httpUtil;
    public static AsyncHttpClient client;
    public static SyncHttpClient syncHttpClient;

    private HttpUtil() {
    }

    public static HttpUtil getInstance() {
        if (httpUtil == null) {
            synchronized (HttpUtil.class) {
                if (httpUtil == null) {
                    return new HttpUtil();
                }
            }
        }
        return httpUtil;
    }

    private static AsyncHttpClient getHttpClient() {
        if (client == null) {
            synchronized (AsyncHttpClient.class) {
                if (client == null) {
                    return new AsyncHttpClient();
                }
            }
        }
        return client;
    }
    private static SyncHttpClient getHttpSyncClient() {
        if (syncHttpClient == null) {
            synchronized (SyncHttpClient.class) {
                if (syncHttpClient == null) {
                    return new SyncHttpClient();
                }
            }
        }
        return syncHttpClient;
    }

    public static void syncReqData(String url, RequestParams params, final Class entityClass, final RequestCallBack
            callBack) {
        try {
            client = getHttpSyncClient();
            Log.d(TAG, entityClass.getName() + "-input " + params.toString());
            Log.d(TAG, entityClass.getName() + "-url " + url);
            client.setTimeout(15 * 1000);
            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    Log.d(TAG, entityClass.getName() + "-Output " + new String(responseBody));  //查看返回数据
                    try {
                        Gson gson = new Gson();
                        BaseOutput out = (BaseOutput) gson.fromJson(new String(responseBody), entityClass);
                        if (callBack != null) {
                            if (out.getCode() == 0) {
                                callBack.onSuccess(out);
                            } else {
                                callBack.onFalse(out.getMsg());
                            }
                        }
                    } catch (Exception e) {
                        if (callBack != null) {
                            callBack.onFalse("数据解析失败");
                        }
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    if (error != null && error.getMessage() != null) {
                        Log.d(TAG, error.toString());
                    }
                    if (callBack != null) {
                        callBack.onFalse(error.toString());
                    }}});} catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadData(String url, RequestParams params, final Class entityClass, final RequestCallBack
            callBack) {
        try {
            client = getHttpClient();
            params.put("number", BaseParam.getDeviceId());
            Log.d(TAG, entityClass.getName() + "-input " + params.toString());
            Log.d(TAG, entityClass.getName() + "-url " + url);
            client.setTimeout(10 * 1000);
            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    Log.d(TAG, entityClass.getName() + "-Output " + new String(responseBody));  //查看返回数据
                    try {
                        Gson gson = new Gson();
                        BaseOutput out = (BaseOutput) gson.fromJson(new String(responseBody), entityClass);
                        if (callBack != null) {
                            if (out.getCode() == 0) {
                                callBack.onSuccess(out);
                            } else {
                                callBack.onFalse(out.getMsg());
                            }

                        }
                    } catch (Exception e) {
                        if (callBack != null) {
                            Log.i("mycode","错误："+e.getMessage());
                            callBack.onFalse("数据解析失败");
                        }
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    if (error != null && error.getMessage() != null) {
                        Log.d(TAG, error.toString());
                    }
                    if (callBack != null) {
                        callBack.onFalse(error.toString());
                    }}});} catch (Exception e) {
            e.printStackTrace();
        }
    }
}
